import React, { Component } from 'react'

// 引入数据源
import store from './store'
// 得到action
import { incr, decr } from './store/actions/counterAction'


export default class App extends Component {

  constructor(props) {
    super(props);
    /* this.state = {
      count:store.getState().count
    } */
    this.state = store.getState()

    // 订阅
    /* store.subscribe(()=>{
      this.setState(state=>{
        return {
          count:store.getState().count
        }
      })
    }) */

    store.subscribe(() => {
      // 如果有数据修改，则会自己更新状态数据
      this.setState(state => store.getState())
    })
  }

  render() {
    return (
      <div>
        <h3>{this.state.count}</h3>
        <button onClick={this.incr}>+++</button>
        <button onClick={this.decr}>---</button>
      </div>
    )
  }
  incr = () => {
    // 派发一个任务给reducer 给一个任务清单
    store.dispatch(incr())
  }
  decr = () => {
    // 派发一个任务给reducer 给一个任务清单
    store.dispatch(decr())
  }

}
